var fun ={
	//用于绑定事件  仅执行一次
	bindOnce:function(domId,action,hanlder){
		$(domId).unbind(action).bind(action,function(event){
			hanlder(event);
		}); 
	},
	tan:function(text2){
		$("#tan2").addClass("tan");
		$("#tan2").text(text2);
		clearTimeout(a);
		var a = setTimeout (function(){
			ding();
		},2000);
		function ding (){
			$("#tan2").removeClass("tan");
			$("#tan2").text("");
		};
	},
	tan2:function(title){
		$("#tan3").show();
		$("#tan3 h2").text(title);
	},
	//存储
	setCache : function(key,value){
		localStorage.setItem(key,JSON.stringify(value));
	},
	muitan:function(title,info){ 
		$(".mui-popup-title").hide();
		$("#SR_input").show();
		$(".rgba").show();
		$("#SR_Title").text(title);
		$(".mui-popup-input input").val("").attr("placeholder",info);
		$(".btn1").click(function(){
			$(".mui-popup").hide();
			$(".rgba").hide();
		});
		$(".btn2").unbind("click").click(function(){
			var content2 = $(".mui-popup-input input").val(); 
			$('#'+$('#popid').val()+' span:last-child').text(content2); 
			$(".btn1").click();
		});
	}, 
	//取缓存  keys 键 
	getCache : function(key){
		if(localStorage.getItem(key)){
			try{
				return JSON.parse(localStorage.getItem(key));
			}catch(e){
				return false;
			}
		}else{
			return false;
		}
	},
	isnull : function (z){
		return !z || z === null || typeof(z) === undefined ?  false : true ;
	},
	ages : function(str){
    	var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if(r==null) return false;
        var d = new Date(r[1],r[3]-1,r[4]);
        if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]){
            var Y = new Date().getFullYear();
            return(Y-r[1]);
        }
        return("输入的日期格式错误！");
	},
	//删除缓存 keys
	remove : function(key){
		localStorage.removeItem(key);
	},
	
	base64encode:function(str) {
		var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var base64DecodeChars = new Array(
	　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	　　52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	　　-1,　0,　1,　2,　3,  4,　5,　6,　7,　8,　9, 10, 11, 12, 13, 14,
	　　15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
	　　-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	　　41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
	　　var out, i, len;
	　　var c1, c2, c3;
	　　len = str.length;
	　　i = 0;
	　　out = "";
	　　while(i < len) {
	 c1 = str.charCodeAt(i++) & 0xff;
	 if(i == len)
	 {
	　　 out += base64EncodeChars.charAt(c1 >> 2);
	　　 out += base64EncodeChars.charAt((c1 & 0x3) << 4);
	　　 out += "==";
	　　 break;
	 }
	 c2 = str.charCodeAt(i++);
	 if(i == len)
	 {
	　　 out += base64EncodeChars.charAt(c1 >> 2);
	　　 out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	　　 out += base64EncodeChars.charAt((c2 & 0xF) << 2);
	　　 out += "=";
	　　 break;
	 }
	 c3 = str.charCodeAt(i++);
	 out += base64EncodeChars.charAt(c1 >> 2);
	 out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	 out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
	 out += base64EncodeChars.charAt(c3 & 0x3F);
	　　}
　　return out;
},
 base64decode:function(str) {
 	var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var base64DecodeChars = new Array(
	　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	　　52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	　　-1,　0,　1,　2,　3,  4,　5,　6,　7,　8,　9, 10, 11, 12, 13, 14,
	　　15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
	　　-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	　　41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
		　　var c1, c2, c3, c4;
		　　var i, len, out;
		　　len = str.length;
		　　i = 0;
		　　out = "";
		　　while(i < len) {
		 /* c1 */
		 do {
		　　 c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
		 } while(i < len && c1 == -1);
		 if(c1 == -1)
		　　 break;
		 /* c2 */
		 do {
		　　 c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
		 } while(i < len && c2 == -1);
		 if(c2 == -1)
		　　 break;
		 out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
		 /* c3 */
		 do {
		　　 c3 = str.charCodeAt(i++) & 0xff;
		　　 if(c3 == 61)
		　return out;
		　　 c3 = base64DecodeChars[c3];
		 } while(i < len && c3 == -1);
		 if(c3 == -1)
		　　 break;
		 out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
		 /* c4 */
		 do {
		　　 c4 = str.charCodeAt(i++) & 0xff;
		　　 if(c4 == 61)
		　return out;
		　　 c4 = base64DecodeChars[c4];
		 } while(i < len && c4 == -1);
		 if(c4 == -1)
		　　 break;
		 out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
		　　}
		　　return out;
	},
	bindLinkToRouter:function(domId){
		$(domId).unbind('click').bind('click',function(event){
			
			var target = event.target;
			for (; target && target !== document; target = target.parentNode) {
				if (target.tagName === 'A' && target.hash) {
					var currId = viewApi.getCurPageId();
					var pageParm = target.hash.replace('#', '');
					pageParm = pageParm.split('/');
					pageParm.shift();
					var pageId = pageParm.shift(); 
					if(currId==pageId){ 
						event.preventDefault();
						return false;
					}else{
						var page = document.getElementById(pageId); 
						if (page && page.classList.contains("mui-page")) { 
							event.stopPropagation(); 
							viewApi.go('#'+pageId,pageParm);
							break;
						} 
					} 
				}
			}
			event.preventDefault();
			return false;
		});
	},
	photo:function() {
		mui.plusReady(function() {
        var cmr = plus.camera.getCamera();  
        var res = cmr.supportedImageResolutions[0];  
        var fmt = cmr.supportedImageFormats[0];  
        cmr.captureImage(function(path) {    
        	void plus.gallery.save(path);
            plus.io.resolveLocalFileSystemURL(path, function(entry){
            	var url = entry.toLocalURL();
            	var image = new Image();
            	//获取照片方向角属性，用户旋转控制  
            	var canvas = document.createElement("canvas");  
			    image.onload = function() {
			      EXIF.getData(this, function(){
			  		Orientation = EXIF.getTag(this, 'Orientation');
				  	if (navigator.userAgent.match(/iphone/i)){
	                    //如果方向角不为1，都需要进行旋转
	                    if(Orientation != "" && Orientation != 1 && Orientation){
	                        switch(Orientation){  
	                            case 6://需要顺时针（向左）90度旋转  
	                                rotateImg(this,'left',canvas);  
	                                break;  
	                            case 8://需要逆时针（向右）90度旋转  
	                                rotateImg(this,'right',canvas);  
	                                break;  
	                            case 3://需要180度旋转  
	                                rotateImg(this,'right',canvas);//转两次  
	                                rotateImg(this,'right',canvas);  
	                                break; 
	                        }         
	                    }
	                }else if (navigator.userAgent.match(/Android/i)) {// 修复android 
//	                	console.log(navigator.userAgent.match(/Android/i));
	                	if(Orientation != "" && Orientation != 1 && Orientation){
	                        switch(Orientation){
	                            case 6://需要顺时针（向左）90度旋转  
	                                rotateImg(this,'left',canvas);  
	                                break;  
	                            case 8://需要逆时针（向右）90度旋转  
	                                rotateImg(this,'right',canvas);  
	                                break;  
	                            case 3://需要180度旋转  
	                                rotateImg(this,'right',canvas);//转两次  
	                                rotateImg(this,'right',canvas);  
	                                break;  
	                        }         
	                    }else{
	                    	appendFile(url,function(dat){
			            		$('#imgView').attr('src', dat).attr('d_data',dat);
			            		pageFun.closeLoading();
				            	$('#imgView').cropper({
										movable:false, 
										resizable:false,
										aspectRatio:1/1,
										highlight:false,//Boolean，默认值true。是否在剪裁框上显示白色的模态窗口。
										guides:false,//Boolean，默认值true。是否在剪裁框上显示虚线。
								});
			            	}); 
	                    }
                	}else{
                		 if(Orientation != "" && Orientation != 1){
                        //alert('旋转处理');
                        switch(Orientation){
                            case 6://需要顺时针（向左）90度旋转
                                rotateImg(this,'left',canvas);
                                break;
                            case 8://需要逆时针（向右）90度旋转
                                rotateImg(this,'right',canvas);
                                break;
                            case 3://需要180度旋转
                                rotateImg(this,'right',canvas);//转两次
                                rotateImg(this,'right',canvas);  
                                break;  
                        }
                   	}
                    dat = canvas.toDataURL("image/jpeg", 0.8);
                	}
				 });
			  };
			    image.src = url;
            });
        }, function(err){
        }, {
            index: 1
        });  
        });
        //对图片旋转处理
		function rotateImg(img, direction,canvas) {
		    //最小与最大旋转方向，图片旋转4次后回到原方向    
		    var min_step = 0;    
		    var max_step = 3;    
		    if (img == null)return;    
		    //img的高度和宽度不能在img元素隐藏后获取，否则会出错    
		    var height = img.height;    
		    var width = img.width;    
		    var step = 2;    
		    if (step == null) {    
		        step = min_step;    
		    }    
		    if (direction == 'right') {    
		        step++;    
		        //旋转到原位置，即超过最大值    
		        step > max_step && (step = min_step);    
		    } else {    
		        step--;    
		        step < min_step && (step = max_step);    
		    }    
		    //旋转角度以弧度值为参数    
		    var degree = step * 90 * Math.PI / 180;    
		    var ctx = canvas.getContext('2d');    
		    switch (step) {    
		        case 0:    
		            canvas.width = width;    
		            canvas.height = height;    
		            ctx.drawImage(img, 0, 0);    
		            break;    
		        case 1:    
		            canvas.width = height;    
		            canvas.height = width;    
		            ctx.rotate(degree);    
		            ctx.drawImage(img, 0, -height);    
		            break;    
		        case 2:    
		            canvas.width = width;    
		            canvas.height = height;    
		            ctx.rotate(degree);    
		            ctx.drawImage(img, -width, -height);    
		            break;    
		        case 3:    
		            canvas.width = height;    
		            canvas.height = width;    
		            ctx.rotate(degree);    
		            ctx.drawImage(img, -width, 0);    
		            break;    
		    }
		    dat = canvas.toDataURL("image/jpeg", 0.8);  
		    var k = '<div class="clearfix ctt2"><i class="header-img" style="background-image:url(' + myimg + ');"></i><div class="content content2"><i class="icon-bgd"></i><img src="'+dat+'" /></div></div>';
        	$("#main").append(k);
        	var from = fun.getCache("hxUser");
			if(plp.length > 11){
				var type = "chatgroups";
			}else{
				var type = 'users';
			}
            hx.post(hx.sendImage,{data:dat,from:from,people:plp,target_type:type},function(obj){
	        	if(obj.s){
	        		fun.tan("发送成功");
					$("#main").scrollTop(9999999999999999);
					pageFun.click_img();
	        	}
	        })
		};
    },
    luhnCheck:function (bankno) {
	    var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhn进行比较）
	    var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
	    var newArr = new Array();
	    for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
	        newArr.push(first15Num.substr(i, 1));
	    }
	    var arrJiShu = new Array(); //奇数位*2的积 <9
	    var arrJiShu2 = new Array(); //奇数位*2的积 >9
	    var arrOuShu = new Array(); //偶数位数组
	    for (var j = 0; j < newArr.length; j++) {
	        if ((j + 1) % 2 == 1) { //奇数位
	            if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
	            else arrJiShu2.push(parseInt(newArr[j]) * 2);
	        } else //偶数位
	        arrOuShu.push(newArr[j]);
	    }
	
	    var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
	    var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
	    for (var h = 0; h < arrJiShu2.length; h++) {
	        jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
	        jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
	    }
	
	    var sumJiShu = 0; //奇数位*2 < 9 的数组之和
	    var sumOuShu = 0; //偶数位数组之和
	    var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
	    var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
	    var sumTotal = 0;
	    for (var m = 0; m < arrJiShu.length; m++) {
	        sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
	    }
	
	    for (var n = 0; n < arrOuShu.length; n++) {
	        sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
	    }
	
	    for (var p = 0; p < jishu_child1.length; p++) {
	        sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
	        sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
	    }
	    //计算总和
	    sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
	
	    //计算luhn值
	    var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
	    var luhn = 10 - k;
	
	    if (lastNum == luhn) {
	        $("#banknoInfo").html("luhn验证通过");
	        return true;
	    } else {
	        $("#banknoInfo").html("银行卡号必须符合luhn校验");
	        return false;
	    }
	}
	
}